package com.xbongbong.help.log;

import com.alibaba.fastjson.JSONObject;
import com.xbongbong.paas.constant.PaasConstant;
import com.xbongbong.paas.enums.FieldTypeEnum;
import com.xbongbong.paas.field.FieldAttrEntity;
import com.xbongbong.paas.pojo.AttrValuePojo;
import com.xbongbong.paas.toolbox.util.DateUtil;
import com.xbongbong.saas.service.toolbox.help.FundSetHelp;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;
import java.util.Objects;

/**
 * @author 吴峰
 * @date 2023/02/16 10:10
 */
@Component
public class UpdateBatchLogHelp {

    @Resource
    private FundSetHelp fundSetHelp;

    /**
     * 获取日志信息 -- 字段改变后的值
     * @param explainMap
     * @param attr
     * @param value
     * @return
     */
    public  AttrValuePojo getLogPojo(Map<String, FieldAttrEntity> explainMap, String attr, Object value) {

        FieldAttrEntity fieldAttrEntity = explainMap.get(attr);
        Integer fieldType = fieldAttrEntity.getFieldType();
        String logValue = "";
        String attrName = fieldAttrEntity.getAttrName();
        if (Objects.isNull(value)) {
            return new AttrValuePojo(attr, logValue, attrName);
        }

        if (Objects.equals(FieldTypeEnum.DATETIME.getType(), fieldType)) {
            Long date = Long.valueOf(value.toString());
            if (Objects.equals(date.toString().length(), 10)) {
                date = date * 1000;
            }
            SimpleDateFormat simpleDateFormat = new SimpleDateFormat(fieldAttrEntity.getDateType());
            logValue = DateUtil.getString(date, simpleDateFormat);
        } else if (Objects.equals(FieldTypeEnum.COMBO.getType(), fieldType)
                || Objects.equals(FieldTypeEnum.RADIO_GROUP.getType(), fieldType)) {
            LinkedHashMap<String, String> dataValueMap = (LinkedHashMap<String, String>) value;
            for (Map.Entry<String, String> entry : dataValueMap.entrySet()) {
                if (!Objects.equals(entry.getKey(), "text")) {
                    continue;
                }
                logValue = entry.getValue();
            }
        } else if (Objects.equals(1, fieldAttrEntity.getIsRedundant())
                || (Objects.equals(FieldTypeEnum.STAR.getType(), fieldType))
                || (Objects.equals(FieldTypeEnum.NUM.getType(), fieldType))) {

            // 下拉复选框
            if ((Objects.equals(FieldTypeEnum.COMBO_CHECK.getType(), fieldType))
                    || Objects.equals(FieldTypeEnum.CHECKBOX_GROUP.getType(), fieldType)) {
                List<String> arrList = new ArrayList<>(PaasConstant.DEFAULT_INITIAL_CAPACITY);
                List<LinkedHashMap<String, String>> dataValueMapList = (List<LinkedHashMap<String, String>>) value;
                for (LinkedHashMap<String, String> dataValueMap : dataValueMapList) {
                    for (Map.Entry<String, String> entry : dataValueMap.entrySet()) {
                        if (!Objects.equals(entry.getKey(), "text")) {
                            continue;
                        }
                        String dataValue = entry.getValue();
                        arrList.add(dataValue);
                    }
                }
                logValue = StringUtils.join(arrList, "，");
            } else {
                // 自定义字段
                logValue = value.toString();
            }
        } else if (Objects.equals(FieldTypeEnum.ACCOUNT_PERIOD.getType(), fieldType)) {
            //代表是应收账期
            String receivablePeriod = JSONObject.toJSONString(value);
            logValue = fundSetHelp.getFormatReceivablePeriod(receivablePeriod);
            //TODO 放到了和过滤下拉框那里一块了
//          formDataUpdateBatchDTO.setValue(receivablePeriod);
        } else {
            logValue = value.toString();
        }
        return new AttrValuePojo(attr, logValue, attrName);
    }
}
